import schedule from "node-schedule";
import { getAllQtClist, getQtKlines } from "../http/api/east-money";
import { closeProxy, getProxyAgent, startProxy } from "../utils/proxy";
import {
  formatKLineStrToKLineObj,
  formatSimpleInfosToInfoObj,
} from "../utils/format";
import dayjs from "dayjs";
import { getKlineCodesByDate, saveKline, saveKlines } from "../dao/kline";
import { Kline } from "../entitys/Kline";
import { loop } from "../utils/loop";

export const updateKlinesByQtClist = async () => {
  // await startProxy();
  const { data } = await getQtKlines("1.000001", await getProxyAgent());
  const shKlines = data?.data?.klines;
  const lastKline = formatKLineStrToKLineObj(shKlines?.[shKlines?.length - 1]);
  const date = dayjs(lastKline?.date).format("YYYYMMDD");
  const existedCodes = (await getKlineCodesByDate(date))?.map(
    (item) => item.code
  );

  if (date === dayjs().format("YYYYMMDD")) {
    const allQtList = await getAllQtClist({ agent: await getProxyAgent() });
   
    // await closeProxy();

    const existedCodes = (await getKlineCodesByDate(date))?.map(
      (item) => item.code
    );
   
    const needUpdateCodes = allQtList?.filter(
      (item) => !existedCodes?.includes(item.f12)
    );
    
    console.log("需要更新的股票数量", needUpdateCodes?.length);

    return saveKlines(
      needUpdateCodes?.filter((item:any)=>+item?.f8>0)?.map((qt: any) => {
        const data = formatSimpleInfosToInfoObj(qt);
        const {
          newest: close,
          code,
          open,
          high,
          low,
          volume,
          amount,
          amplitude,
          chg,
          change,
          turn,
        } = data;

        return {
          close,
          code,
          date,
          open,
          high,
          low,
          volume,
          amount,
          amplitude,
          chg,
          change,
          turn,
        } as Kline;
      })
    ).then(async () => {
      console.log(`所有股票更新${date}的K线数据完成`);
    });

    // return loop(needUpdateCodes, 10, async (qt) => {
    //   const data = formatSimpleInfosToInfoObj(qt);
    //   const {
    //     newest: close,
    //     code,
    //     open,
    //     high,
    //     low,
    //     volume,
    //     amount,
    //     amplitude,
    //     chg,
    //     change,
    //     turn,
    //   } = data;

    //   await saveKline({
    //     close,
    //     code,
    //     date,
    //     open,
    //     high,
    //     low,
    //     volume,
    //     amount,
    //     amplitude,
    //     chg,
    //     change,
    //     turn,
    //   } as Kline);
    //   console.log(`保存成功：${code} ${date}`);
    // }).then(()=>{
    //     console.log(`所有股票更新${date}的K线数据完成`);
    // });
    // return await Promise.all(
    //   allQtList?.map(async (qt) => {

    //   })
    // ).then(()=>{
    //     console.log(`所有股票更新${date}的K线数据完成`);
    // });
  }
};

schedule.scheduleJob("0 10 15 * * 1-5", async function () {
  await updateKlinesByQtClist();
});
